package com.jk.smartelderlycare.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jk.smartelderlycare.dao.SysRoleMapper;
import com.jk.smartelderlycare.dto.SysRole;
import com.jk.smartelderlycare.dto.SysUser;
import com.jk.smartelderlycare.pojo.PagedResponse;
import com.jk.smartelderlycare.service.SysRoleService;
import com.jk.smartelderlycare.utils.PaginationUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @author hhk
 * @version 1.0
 * @ClassName SysRoleServiceImpl
 * @program: smart-elderly-care
 * @description: TODO
 * @date 2024/11/7 11:42
 */
@Service
public class SysRoleServiceImpl implements SysRoleService {

    @Autowired
    private SysRoleMapper sysRoleMapper;
    /**
     * @description: 查询角色列表
     * @param: [sysRole]
     * @return: java.util.List<com.jk.smartelderlycare.dto.SysRole>
     * @author hhk
     * @date: 2024/11/7 11:45
     */
    @Override
    public PagedResponse<SysRole> findSysSysRolePaginationList(SysRole sysRole, Integer pageNum, Integer pageSize) {
        Page<SysRole> page = new Page<>(pageNum,pageSize);
        IPage<SysRole> sysRolePageList = sysRoleMapper.findSysSysRolePaginationList(sysRole,page);
        return PaginationUtil.getPagedResponse(sysRolePageList.getRecords(),pageNum,pageSize,sysRolePageList.getTotal());
    }

    /**
     * @description: 新增角色列表
     * @param: [sysRole]
     * @return: void
     * @author hhk
     * @date: 2024/11/7 11:46
     */
    @Override
    public void addSysRole(SysRole sysRole) {
        sysRole.setCreateDate(new Date());
        sysRoleMapper.insert(sysRole);
    }
    /**
     * @description: 根据id删除一条数据
     * @param: [roleId]
     * @return: void
     * @author hhk
     * @date: 2024/11/7 11:46
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteSysRoleByRoleId(Long roleId) {
        sysRoleMapper.deleteById(roleId);
        //删除中间表
        sysRoleMapper.deleteRoleAndUserByRoleId(roleId);
    }
    /**
     * @description: 根据id回显一条数据
     * @param: [roleId]
     * @return: com.jk.smartelderlycare.dto.SysRole
     * @author hhk
     * @date: 2024/11/7 11:47
     */
    @Override
    public SysRole findSysRoleByRoleId(Long roleId) {
        return sysRoleMapper.selectById(roleId);
    }
    /**
     * @description: 修改角色列表
     * @param: [sysRole]
     * @return: void
     * @author hhk
     * @date: 2024/11/7 11:47
     */
    @Override
    public void updateSysRole(SysRole sysRole) {
        sysRoleMapper.updateById(sysRole);
    }
    /**
     * @description: 状态修改
     * @param: [sysRole]
     * @return: void
     * @author hhk
     * @date: 2024/11/7 16:46
     */
    @Override
    public void updateStatusById(SysRole sysRole) {
        sysRoleMapper.updateStatusById(sysRole);
    }
}
